﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;
using BusinessRule.Common;

namespace MesServiceMonitor
{
   public class CommonMethod
    {
       /// <summary>
       /// 启动服务
       /// </summary>
       /// <param name="serviceName"></param>
       public static void startService(string serviceName) {
           // 创建ServiceController实例
           ServiceController sc = new ServiceController(serviceName);
           // 启动服务
           if (sc.Status != ServiceControllerStatus.Running)
           {
               sc.Start();
               LogHelp.WriteLog("启动服务" + serviceName + "中……");

               // 等待服务达到运行状态
               sc.WaitForStatus(ServiceControllerStatus.Running);
               LogHelp.WriteLog( "启动服务" + serviceName + "成功");
           }
       }

       
       /// <summary>
       /// 停止服务
       /// </summary>
       /// <param name="serviceName"></param>
       public static void stopService(string serviceName)
       {
           // 创建ServiceController实例
           ServiceController sc = new ServiceController(serviceName);
           // 停止服务
           if (sc.CanStop)
           {
               sc.Stop();
               LogHelp.WriteLog("停止服务" + serviceName + "中……");

               // 等待服务停止
               sc.WaitForStatus(ServiceControllerStatus.Stopped);
               LogHelp.WriteLog("停止服务" + serviceName + "成功");
           }
       }
    }
}
